//
//  HZMItemsDetailsVC.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/11/2.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMItemsDetailsVC.h"

@interface HZMItemsDetailsVC ()<UIScrollViewDelegate,UIWebViewDelegate>

@property(nonatomic, retain) HZMItemsDetailsModel *myData;
@property(nonatomic, retain) NSMutableArray *imageArr;
@property(nonatomic, retain) HZMItemsDetailsModel *sourceDic;
@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIScrollView *subScrollView;
@property(nonatomic, retain) UIWebView *webView;
@property(nonatomic, retain) UIView *descView;

@end

@implementation HZMItemsDetailsVC
{
    UIActivityIndicatorView *acView;
}

- (void)dealloc
{
    [_keyId release];
    [_myData release];
    [_descView release];
    [_imageArr release];
    [_sourceDic release];
    [_scrollView release];
    [_subScrollView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavigation];
//    23/15
    [self createScrollView];
    
    [self createData];
}



#pragma mark ScrollView Setting
- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    
    self.subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 27 * HEIGHT / 41)];
    [self.scrollView addSubview:self.subScrollView];
    [_subScrollView release];
}

- (void)createSubScrollData
{
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 90, 26 * HEIGHT / 41, WIDTH, 5 * HEIGHT / 41)];
    nameLabel.text = [self.myData name];
    [self.scrollView addSubview:nameLabel];
    [nameLabel release];
    
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 90, 28 * HEIGHT / 41, WIDTH, 5 * HEIGHT / 41)];
    priceLabel.text = [NSString stringWithFormat:@"🐑%@", [self.myData price]];
    priceLabel.textColor = [UIColor redColor];
    priceLabel.font = [UIFont systemFontOfSize:18.0];
    [self.scrollView addSubview:priceLabel];
    [priceLabel release];
    //用高亮写
    
    UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 90, 32 * HEIGHT / 41, WIDTH, 11 * HEIGHT / 41)];
    descLabel.text = [self.myData myDescription];
    descLabel.font = [UIFont systemFontOfSize:13.0];
    descLabel.textColor = [UIColor grayColor];
    descLabel.numberOfLines = 0;
    [descLabel sizeToFit];
    [self.scrollView addSubview:descLabel];
    [descLabel release];
}



#pragma mark SetNavigation
- (void)setNavigation
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.title = @"单品详情";
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
}

#pragma mark Navigationbar ButtonAction
- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark WebView Setting
- (void)createWebView
{
    [self createScrollViewImageView];
    
    self.scrollView.contentSize = CGSizeMake(0, 2 * HEIGHT);
    self.scrollView.showsVerticalScrollIndicator = YES;
    self.scrollView.pagingEnabled = NO;
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 9 * HEIGHT / 10, WIDTH, HEIGHT)];
    [self.scrollView addSubview:self.webView];
    [_webView release];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    // 风火轮
    acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    acView.center = self.view.center;
    [self.view addSubview:acView];
    
    [self.webView loadHTMLString:[self.myData detail_html] baseURL:nil];
}

#pragma mark WebView Delegate Method
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [acView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [acView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    // 忽略异步加载返回 -999
    if ([error code] == NSURLErrorCancelled) return;
    
//    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"连接错误" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"返回上一页面" otherButtonTitles:@"重新加载", nil];
//    [alerView show];
}



#pragma mark ScrollViewImageView
- (void)createScrollViewImageView
{
    self.subScrollView.pagingEnabled = YES;
    self.subScrollView.showsHorizontalScrollIndicator = NO;
    self.subScrollView.bounces = NO;
    self.subScrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.subScrollView.delegate = self;
    
    if (self.imageArr.count > 1) {
        self.subScrollView.contentSize = CGSizeMake((self.imageArr.count + 2) * WIDTH, 0);
        
        UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 27 * HEIGHT / 41)];
        [firstImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[self.imageArr.count - 1]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
        [self.subScrollView addSubview:firstImageView];
        [firstImageView release];
        firstImageView.userInteractionEnabled = YES;
        firstImageView.tag = 1001;
        
        for (NSInteger i = 1; i <self.imageArr.count + 1 ; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, 27 * HEIGHT / 41)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i - 1]]placeholderImage:[UIImage imageNamed:@"back.gif"]];
            [self.subScrollView addSubview:imageView];
            [imageView release];
            imageView.userInteractionEnabled = YES;
            imageView.tag = 1001 + i;
        }
        
        UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.imageArr.count + 1) * WIDTH, 0, WIDTH, 27 * HEIGHT / 41)];
        [lastImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[0]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
        [self.subScrollView addSubview:lastImageView];
        [lastImageView release];
        lastImageView.userInteractionEnabled = YES;
        lastImageView.tag = self.imageArr.count + 2;
        
    } else {
        UIImageView *oneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, 27 * HEIGHT / 41)];
        [oneImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[0]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
        [self.subScrollView addSubview:oneImageView];
        [oneImageView release];
    }
}

#pragma mark ScrollView Delegate Method
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.imageArr.count > 1) {
        
        if (self.subScrollView.contentOffset.x >= (self.imageArr.count + 2 - 1) * WIDTH) {
            self.subScrollView.contentOffset = CGPointMake(WIDTH, 0);
        }
        if (self.subScrollView.contentOffset.x <= 0) {
            self.subScrollView.contentOffset = CGPointMake((self.imageArr.count) * WIDTH, 0);
        }
    } else {
        
    }
}



#pragma mark Data
- (void)createData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%@", self.keyId] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.myData = [[[HZMItemsDetailsModel alloc] init] autorelease];
        [self.myData setValuesForKeysWithDictionary:dic[@"data"]];
        self.sourceDic = [[[HZMItemsDetailsModel alloc] init] autorelease];
        [self.sourceDic setValuesForKeysWithDictionary:[self.myData source]];
        self.imageArr = [NSMutableArray array];
        self.imageArr = [self.myData image_urls];
        
        [self createScrollViewImageView];
        [self createWebView];
        [self createSubScrollData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
